﻿using Service.src.NW.Model;
using Service.src.NW.Model.UserPageProject;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Service.src.Flow.Server
{
    public class UserPageProjectServer
    {
        internal UserPageProject AddProject(int DBIndex, UserPageProject Project)
        {
            Project = AppData.DatabaseCache[DBIndex].Install(Project);
            return Project;
        }

        internal bool DeleteProject(int DBIndex, int id)
        {
            UserPageProject Project = new UserPageProject();
            Project.Id = id;
            return AppData.DatabaseCache[DBIndex].Delete(Project);
        }

        internal int ReleasedProject(int DBIndex, int id)
        {
            List<UserPageProject> projects = SearchProject(DBIndex);
            foreach (UserPageProject item in projects)
            {
                if (item.Id == id)
                {
                    item.Released = item.Released == 1 ? 0 : 1;
                }
                else
                {
                    item.Released = 0;
                }
            }
            UserPageProject Project = AppData.DatabaseCache[DBIndex].Query<UserPageProject>(s => s.Id == id).FirstOrDefault();
            int r = AppData.DatabaseCache[DBIndex].Update<UserPageProject>(projects);
            return r;
        }

        internal List<UserPageProject> SearchProject(int DBIndex)
        {
            List<UserPageProject> list = AppData.DatabaseCache[DBIndex].QueryAll<UserPageProject>();
            return list;
        }

        internal int UpdateProject(int DBIndex, UserPageProject Project)
        {
            int i = AppData.DatabaseCache[DBIndex].Update(Project);
            return i;
        }

        internal UserPageProject GetProject(int DBIndex,int ProjectId)
        {
            UserPageProject Get = AppData.DatabaseCache[DBIndex].QueryOne<UserPageProject>((u => u.Id == ProjectId));
            return Get;
        }

        internal UserPageProject CopyUserProject(int DBIndex, int ProjectId)
        {
            UserPageProject Get = GetProject(DBIndex,ProjectId);
            UserPageProject New = new UserPageProject();
            New.Title = Get.Title +  DateTime.Now+"-副本";
            New.Preview = Get.Preview;
            New.Released = 0;
            New.Label = Get.Desc;
            New.Desc = Get.Desc;
            New = AddProject(DBIndex, New);

            return New;
        }
    }
}
